package net.lzzy.practices.repositories;

import net.lzzy.practices.entities.User;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.util.List;
import java.util.UUID;


/**
 * @author zly on 2022/5/9
 * Description:
 */

public interface UserRepository extends JpaRepository<User, UUID> {
    /**
     * 根据用户查找用户名和密码
     * @param userName 用户名
     * @param password 密码
     * @return
     */
    User findUserByUserNameAndPassword(String userName, String password);

    /**
     * @param userName 用户名
     * @return
     */
    Integer countByUserName(String userName);
    Integer countAllBy();
    List<User> findAllByOrderByUpdateTimeDesc();
    List<User> findAllByOrderByUpdateTimeDesc(Pageable pageable);
    List<User> findUsersByApplyTeacherOrderByUpdateTimeDesc(boolean applying);
    User findUserByType(int type);

    @Query("select u from User u where u.userName like " + "concat('%',:kw,'%') or u.nickName like " +
            "concat('%',:kw,'%') " + "or u.email like concat('%',:kw,'%') or u.phone like concat('%',:kw,'%')")
    List<User> findByQuery(@Param("kw") String kw);
    /**
     * 返回指定用户名用户的投影（salt）
     * @param userName
     * @return UserProjection
     */
    List<UserProjection>findUsersByUserName(String userName);

}